<?php
if ( IPB_THIS_SCRIPT != 'admin' )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'init.php'.";
	exit();
}

class run_upgrade
{
	var $ipsclass;
	var $version_id;
	var $version_name;
	
	function auto_run()
	{
		$redirect = array(
							'0' => 'create_tables',
							'1' => 'alter_tables',
							'2' => 'alter_member_table',
							'3' => 'update_category_order',
							'4' => 'update_transfer_settings',
							'5' => 'update_ban_item',
						);
		
		if(!isset($redirect[$this->ipsclass->input['step']]))
		{
			$this->ipsclass->input['step'] = 0;
		}
		
		$func = $redirect[$this->ipsclass->input['step']];
		$this->$func();
	}
	
	function create_tables()
	{
		$this->ipsclass->DB->load_cache_file(ROOT_PATH.'sources/market/install/'.$this->ipsclass->vars['sql_driver'].'_database_queries.php','sql_database_queries');
		
		$this->ipsclass->DB->build_query_from_cache('table_market_taxes',array(),'sql_database_queries');
		$this->ipsclass->DB->exec_query();
		$this->ipsclass->DB->build_query_from_cache('table_market_tax_log',array(),'sql_database_queries');
		$this->ipsclass->DB->exec_query();
		
		if($this->ipsclass->market_class->count_plugin_filename("quiz.php"))
		{
			$this->ipsclass->DB->load_cache_file(ROOT_PATH.'sources/market/sql/'.$this->ipsclass->vars['sql_driver'].'_plugin_queries.php','sql_plugin_queries');
			
			$this->ipsclass->DB->build_query_from_cache('table_quiz_sessions',array(),'sql_plugin_queries');
			$this->ipsclass->DB->exec_query();
		}
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=1","Installed new tables...");
	}
	
	function alter_tables()
	{
		$this->ipsclass->DB->load_cache_file(ROOT_PATH.'sources/market/install/'.$this->ipsclass->vars['sql_driver'].'_database_queries.php','sql_database_queries');
		
		$this->ipsclass->DB->sql_add_field("groups", "g_market_tax" ,"float(15,10) NOT NULL","0.0000000000");
		
		$this->ipsclass->DB->sql_add_field("market_categories", "cat_order" ,"smallint NOT NULL","0");
		
		$this->ipsclass->DB->sql_add_field("market_inventory", "expired" ,"int NOT NULL","0");
		
		$this->ipsclass->DB->sql_add_field("market_items", "expire_time" ,"int NOT NULL","0");
		$this->ipsclass->DB->sql_add_field("market_items", "expire_unit" ,"int NOT NULL","0");
		
		$this->ipsclass->DB->build_query_from_cache('add_text_field',array('table'=>'market_items','field'=>'settings'),'sql_database_queries');
		$this->ipsclass->DB->exec_query();
		$this->ipsclass->DB->build_query_from_cache('add_text_field',array('table'=>'market_items','field'=>'limit_buy'),'sql_database_queries');
		$this->ipsclass->DB->exec_query();
		$this->ipsclass->DB->build_query_from_cache('add_text_field',array('table'=>'market_items','field'=>'limit_use'),'sql_database_queries');
		$this->ipsclass->DB->exec_query();
		
		if($this->ipsclass->market_class->count_plugin_filename("auction.php"))
		{
			$this->ipsclass->DB->sql_add_field("market_auction_item", "expired", "int NOT NULL" ,"0");
		}
		
		if($this->ipsclass->market_class->count_plugin_filename("userstore.php"))
		{
			$this->ipsclass->DB->sql_add_field("market_user_store_item", "expired", "int NOT NULL" ,"0");
		}
		
		if($this->ipsclass->market_class->count_plugin_filename("transfer.php"))
		{
			$this->ipsclass->DB->sql_add_field("market_transfer_logs", "l_anon", "tinyint NOT NULL" ,"0");
		}
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=2","Altered existing tables...");
	}
	
	function alter_member_table()
	{
		$this->ipsclass->DB->sql_add_field("members", "market_tax" ,"float(15,10) NOT NULL","0.0000000000");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=3","Altered member table...");
	}
	
	function update_category_order()
	{
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		
		$this->update_order();
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=4","Updated category order...");
	}
	
	function update_order($parent = -1)
	{
		if(!is_array($this->ipsclass->market_class->category_tree[$parent]) || count($this->ipsclass->market_class->category_tree[$parent]) == 0)
		{
			return;
		}
		$order = 0;
		foreach(array_keys($this->ipsclass->market_class->category_tree[$parent]) as $cat_id)
		{
			$this->ipsclass->DB->do_update('market_categories',array('cat_order'=>++$order),"cat_id=".$cat_id);
			$this->update_order($cat_id);
		}
	}
	
	function update_transfer_settings()
	{
		if(is_array($this->ipsclass->cache['market_plugin']) && count($this->ipsclass->cache['market_plugin']))
		{
			foreach($this->ipsclass->cache['market_plugin'] as $plugin)
			{
				if($plugin['filename'] == "transfer.php")
				{
					$settings = unserialize($plugin['plugin_config']);
					$settings['allow_anon'] = 0;
					$settings['anon_sub'] = "Anonymous Transferring";
					$settings['anon_msg'] = 
<<<EOF
{member_name_to},

I have sent you:[list]
[*]{currency_name} {money_amount}
[/list]{item_list}

Anonymous

[b]This is an automated message[/b]
EOF;
					$this->ipsclass->DB->do_update('market_plugins',array(
																			'plugin_config' => serialize($settings),
										),"link='".$plugin['link']."'");
				}
			}
		}
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=upgrade&CODE=upgrade&step=5","Updated transfer plugin settings...");
	}
	
	function update_ban_item()
	{
		if(is_array($this->ipsclass->cache['market_items']) && count($this->ipsclass->cache['market_items']))
		{
			foreach($this->ipsclass->cache['market_items'] as $item)
			{
				if($item['filename'] == 'other/item_temporaryban.php')
				{
					$settings = unserialize($item['item_config']);
					$settings['subject'] = 'You have been banned!';
					$settings['message'] = 
<<<EOF
{member_name},

You have been banned from {board_name} starting from {start_time} to {end_time} by {user_name}. You can either wait out the time or have someone use the unban item.
EOF;
					$this->ipsclass->DB->do_update('market_items',array(
																			'item_config' => serialize($settings),
									),"item_id='".$item['item_id']."'");
				}
			}
		}
	}
}
?>